package com.cloud.eurekahystrixprovider.service.impl;

import com.cloud.eurekahystrixprovider.service.ProviderService;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixProperty;
import com.netflix.hystrix.contrib.javanica.conf.HystrixPropertiesManager;
import org.springframework.stereotype.Service;

/**
 * 熔断虚拟实现
 *
 * @author kangwen
 */
@DefaultProperties(defaultFallback = "fallback")
@Service
public class ProviderServiceImpl extends FallbackServiceImpl implements ProviderService {


	@HystrixCommand(commandProperties = {
			@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "2000"),
			@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ENABLED, value = "true"),
			@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_REQUEST_VOLUME_THRESHOLD, value = "10"),
			@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_SLEEP_WINDOW_IN_MILLISECONDS, value = "10000"),
			@HystrixProperty(name = HystrixPropertiesManager.CIRCUIT_BREAKER_ERROR_THRESHOLD_PERCENTAGE, value = "50"),

	})
	@Override
	public String hystrix() {
		try {
			Thread.sleep(3000);
		} catch (InterruptedException e) {
		}
		return "Provider Hystrix response ok " + Thread.currentThread().getName() + "-" + Thread.currentThread()
				.getId();
	}

	@HystrixCommand(commandProperties = {
			@HystrixProperty(name = HystrixPropertiesManager.EXECUTION_ISOLATION_THREAD_TIMEOUT_IN_MILLISECONDS, value = "2000")

	})
	@Override
	public String hello() {
		return "Provider hello " + Thread.currentThread().getName() + "-" + Thread.currentThread().getId();
	}
}
